package com.ai.bitcoin.service.helper;

import com.ai.bitcoin.jpa.dao.BccSubCoinDao;
import com.ai.bitcoin.jpa.dao.BccUserDao;
import com.ai.bitcoin.jpa.dao.BccUserPlcRecordDao;
import com.ai.bitcoin.jpa.entity.BccSubCoin;
import com.ai.bitcoin.jpa.entity.BccUserBonusConfig;
import com.ai.bitcoin.jpa.entity.BccUserPlcRecord;
import com.ai.bitcoin.jpa.service.UserBonusService;
import com.ai.bitcoin.service.MakeHtmlService;
import com.ai.bitcoin.vo.UserEncourageReq;
import com.ai.bitcoin.vo.UserEncourageResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class WeixinServiceUpdateHelper {

    @Autowired
    private UserBonusService userBonusService;

    @Autowired
    private WeiXinServiceHelper weiXinServiceHelper;

    @Autowired
    private BccSubCoinDao bccSubCoinDao;

    @Autowired
    private BccUserDao bccUserDao;

    @Autowired
    private BccUserPlcRecordDao bccUserPlcRecordDao;

    @Autowired
    private MakeHtmlService makeHtmlService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Transactional
    public void registNewUser(String fromUserName, String toUserName, String parentOpenId, String account) {
        //关注
        UserEncourageReq request = new UserEncourageReq();
        request.setOpenId(fromUserName);
        request.setParentOpenId(parentOpenId);
        UserEncourageResponse response = userBonusService.sendBonus(BccUserBonusConfig.EVENT_SOURCE_MP, "0101", request);

        if (CollectionUtils.isNotEmpty(response.getMessageList())) {
            Map<String, Object> messageMap = new HashMap<>();
            messageMap.put("touser", fromUserName);
            messageMap.put("msgtype", "text");
            Map<String, Object> textMap = new HashMap<>();
            textMap.put("content", response.getMessageList().get(0));
            messageMap.put("text", textMap);
            weiXinServiceHelper.sendMsg2Customer(messageMap);
        }
    }

    @Transactional
    public void registSubCoin(String fromUserName, String toUserName, String parentOpenId, String bccId, String bccName) {
        BccSubCoin subCoin = bccSubCoinDao.queryByBccIdAndUserOpenId(bccId, fromUserName);
        if (subCoin == null) {
            BccSubCoin bccSubCoin = new BccSubCoin();
            bccSubCoin.setUserOpenId(fromUserName);
            bccSubCoin.setStatus(1);
            bccSubCoin.setSource("simply");
            bccSubCoin.setEndTime(System.currentTimeMillis() / 1000 + 60 * 60 * 24 * 30);
            Date now1 = new Date();
            bccSubCoin.setUpdateTime(now1);
            bccSubCoin.setCreateTime(now1);
            bccSubCoin.setBccId(bccId);
            bccSubCoin.setBccName(bccName);

            try {
                bccSubCoinDao.save(bccSubCoin);
            } catch (Exception e) {
                log.error("异常回滚{}{}{}", "5", fromUserName, e);
            }
        }
        else {
            //nothing to do
        }

    }

    public void registUserPlcRecord(String openId, String invitedOpenId, String reason, Integer type, Double plc) {
        BccUserPlcRecord bccUserPlcRecord = new BccUserPlcRecord();
        bccUserPlcRecord.setCeateTime(new Date());
        bccUserPlcRecord.setOpenId(openId);
        bccUserPlcRecord.setInvitedOpenId(invitedOpenId);
        bccUserPlcRecord.setReason(reason);
        bccUserPlcRecord.setType(type);
        bccUserPlcRecord.setPlc(plc);
        bccUserPlcRecordDao.save(bccUserPlcRecord);
    }

}
